"""
@Project ：HaloInterfaceProject 
@File    ：UserModuleTest.py
@IDE     ：PyCharm 
@Author  ：张成龙
@Date    ：2024/10/24 16:51 
@explain ：
"""
import logging
import os

import allure
import pytest

from api.UserModule.CreateUser import CreateUser
from api.UserModule.DeleteUser import DeleteUser
from api.UserModule.GetAllUser import GetAllUser
from api.UserModule.GetUserRoles import GetUserRoles
from api.UserModule.UpdateUserPassword import UpdateUserPassword
from api.UserModule.UploadUserAvatar import UploadUserAvatar
from conftest import BASE_DIR
from utils.AllureAttachReqRespUtil import AllureAttachReqRespUtil
from utils.FakerDataUtil import FakerDataUtil


@pytest.mark.run(order=2)
@allure.epic("后台管理")
@allure.feature("用户管理")
@allure.story("用户权限")
@allure.description("用户管理模块")
@pytest.mark.user
class TestUserModule:

    roles_name = None
    user_name = None

    def setup_class(self):
        self.get_user_roles = GetUserRoles()
        self.random_int = FakerDataUtil.random_generate_integer(0, 4)
        self.create_user = CreateUser()
        self.get_all_user = GetAllUser()
        self.upload_user_avatar = UploadUserAvatar()
        self.update_user_password = UpdateUserPassword()
        self.delete_user = DeleteUser()

    @allure.step(title="获取所有用户角色")
    @allure.severity(allure.severity_level.BLOCKER)
    def test_get_user_roles(self):
        response = self.get_user_roles.get_user_roles()

        AllureAttachReqRespUtil.attach_request_response(response.request, response)

        logging.info("get_user_roles response = {}".format(response.text))

        TestUserModule.roles_name = response.json()["items"][self.random_int]["metadata"]["name"]

        print("====================" + self.roles_name + "================")

        assert response.status_code == 200

        assert response.json()["items"][self.random_int]["kind"] == "Role"

    @allure.step(title="新增用户")
    @allure.severity(allure.severity_level.BLOCKER)
    def test_create_user(self):
        response = self.create_user.create_user(TestUserModule.roles_name)

        TestUserModule.user_name = response.json()["metadata"]["name"]

        print("====================" + self.user_name + "======================")

        AllureAttachReqRespUtil.attach_request_response(response.request, response)

        logging.info("create_user response = {}".format(response.text))

        assert response.status_code == 200

        assert response.json()["kind"] == "User"

    @allure.step(title="获取所有用户")
    @allure.severity(allure.severity_level.BLOCKER)
    def test_get_all_user(self):

        response = self.get_all_user.get_all_user()
        AllureAttachReqRespUtil.attach_request_response(response.request, response)

        logging.info("get_all_user response = {}".format(response.text))

        assert response.status_code == 200

        assert response.json()["items"][0]["user"]["kind"] == "User"

    @allure.step(title="上传用户头像")
    @allure.severity(allure.severity_level.BLOCKER)
    def test_upload_user_avatar(self):

        file_path = BASE_DIR + os.sep + "data" + os.sep + "image" + os.sep + "1.png"

        file = open(file_path, "rb")

        response = self.upload_user_avatar.upload_user_avatar(file, TestUserModule.user_name)

        AllureAttachReqRespUtil.attach_request_response(response.request, response)

        logging.info("upload_user_avatar response= {}".format(response.text))
        assert response.status_code == 200
        assert response.json()["kind"] == "User"

    @allure.step(title="修改用户密码")
    @allure.severity(allure.severity_level.BLOCKER)
    def test_update_user_password(self):

        response = self.update_user_password.update_user_password(TestUserModule.user_name)

        AllureAttachReqRespUtil.attach_request_response(response.request, response)
        logging.info("update_user_password response= {}".format(response.text))
        assert response.status_code == 200
        assert response.json()["kind"] == "User"

    @allure.step(title="删除用户")
    @allure.severity(allure.severity_level.BLOCKER)
    def test_delete_user(self):
        response = self.delete_user.delete_user(TestUserModule.user_name)
        AllureAttachReqRespUtil.attach_request_response(response.request, response)
        logging.info("delete_user response = {}".format(response.text))

        assert response.status_code == 200
        assert response.json()["kind"] == "User"










